<template>
  <div class="details">
    <top/>
    <div v-if="hid" class="no-data">
      <img src="@/assets/home/no_data.png" alt="">
      <span>暂时没人有回答哦，快来帮助他吧~<i class="el-icon-loading"/></span>
    </div>
    <div class="commit">
      <div class="commit-det">
        <div class="asklist">
          <ul>
            <li v-for="(item, idx) in detilCon" :key="idx" class="list">
              <div class="listTop">
                <div style="display:flex;align-items:center;">
                  <div class="head">
                    <img src="../../../assets/home/01.jpg" alt>
                  </div>
                  <span style="margin-left:10px;">{{ item.user }}</span>
                  <span style="margin-left:10px;">回答了问题</span>
                </div>
                <span style="float:right">{{ item.time }}</span>
              </div>
              <a class="main">
                <h4 class="Conbt">{{ item.title }}</h4>
                <div class="Context">
                  <p>{{ item.answerCon }}</p>
                </div>
                <div class="botTag">
                  <div style="color:#959595;">
                    <span>
                      <i class="el-icon-view"/>{{ item.look }}个浏览
                    </span>
                    <span>
                      <span class="iconfont">&#xe63c;</span>{{ item.good }}个点赞
                    </span>
                  </div>
                  <div>
                    <el-button type="success" size="mini">回答问题</el-button>
                    <el-button type="warning" size="mini">举报</el-button>
                  </div>
                </div>
              </a>
            </li>
          </ul>
        </div>
        <div class="bot-com">
          <div class="img-head">
            <img src="@/assets/home/01.jpg" alt="">
          </div>
          <div class="text-con">
            <el-input v-model="textCon" type="textarea" rows="5" placeholder="赶快来帮助他吧~"/>
          </div>
          <div class="put-text">
            <el-button type="primary" icon="el-icon-edit" @click="answer">回答</el-button>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import top from './top.vue'
import { gerDetil, addAnser } from '@/api/ask'
import { formatTime } from '@/utils/index.js'
import { mapGetters } from 'vuex'
export default {
  name: 'Details',
  components: {
    top
  },
  data() {
    return {
      detilCon: [],
      hid: 0,
      textCon: ''
    }
  },
  computed: {
    ...mapGetters([
      'name'
    ])
  },
  created: function() {
    const id = this.$route.query.id
    gerDetil(id).then((res) => {
      if (res.data === null || res.data === '') {
        this.hid = 1
      } else {
        let i = 0
        for (i in res.data) {
          res.data[i].time = formatTime(res.data[i].time)
        }
        this.detilCon = res.data
      }
    })
  },
  methods: {
    answer() {
      const obj = {
        'user': this.name,
        'askid': this.$route.query.id,
        'answerCon': this.textCon,
        'time': Math.round(new Date().getTime() / 1000).toString()
      }
      if (obj.user) {
        // 添加问答
        addAnser(obj).then((res) => {
          if (res.success) {
            this.$router.go(0)
          } else {
            this.$alert('添加失败')
          }
        })
      } else {
        this.$alert('请登录您的账号')
      }
    }
  }
}
</script>
<style lang="scss" scoped>
.details {
  height: 100%;
  padding-top: 140px;
  color: #959595;
  background: #fafafa;
}
@media only screen and (min-width: 1024px) {
  .no-data {
    width: 160px;
    height: 200px;
    margin: 0 auto;
    display: flex;
    flex-direction: column;
    text-align: center;
    font-size: 18px;
    color: #353535;
  }
  .commit {
    width: 100%;
    position: relative;
    max-width: 1160px;
    zoom: 1;
    margin-left: auto;
    margin-right: auto;
  }
  .commit-det {
    width: 820px;
    border-top: 1px solid #ececec;
    border-radius: 3px;
    box-shadow: 0 0 1px #fdfdfd;
    .asklist {
      width: 100%;
      ul,
      li {
        margin: 0;
        padding: 0;
        list-style: none;
        font-size: 14px;
      }
      .Conbt {
        font-size: 16px;
        font-weight: 540;
        padding-top: 15px;
      }
      .main {
        .Context {
          font-size: 14px;
          padding-bottom: 20px;
          line-height: 26px;
          p {
            position: relative;
            line-height: 20px;
            max-height: 40px;
            overflow: hidden;
          }
          p::after {
            content: "...";
            position: absolute;
            bottom: 0;
            right: 0;
            padding-left: 40px;
            background: -webkit-linear-gradient(left, transparent, #fff 55%);
            background: -o-linear-gradient(right, transparent, #fff 55%);
            background: -moz-linear-gradient(right, transparent, #fff 55%);
            background: linear-gradient(to right, transparent, #fff 55%);
          }
        }
        .botTag {
          margin-top: 10px;
          width: 100%;
          display: flex;
          height: 40px;
          line-height: 40px;
          justify-content: space-between;
        }
      }
      .list {
        padding: 30px;
        background: #ffffff;
        border: 1px solid #ececec;
        &:hover {
          box-shadow: 0px 1px 1px #f0f0f0;
        }
        .listTop {
          display: flex;
          width: 100%;
          align-items: center;
          justify-content: space-between;
          span:nth-last-child() {
            color: #666;
          }
          .head {
            width: 40px;
            height: 40px;
            border-radius: 20px;
            overflow: hidden;
            img {
              width: 100%;
              height: 100%;
            }
          }
        }
      }
    }
    .bot-com {
      width: 100%;
      height: 210px;
      border-top: 1px solid #ececec;
      padding: 20px;
      margin-top: 15px;
      box-sizing: border-box;
      background: #fff;
      .img-head {
        width: 40px;
        height: 40px;
        border-radius: 20px;
        overflow: hidden;
        float: left;
        img {
          width: 100%;
          height: 100%;
        }
      }
      .text-con {
        width: 680px;
        height: 90px;
        float: left;
        margin-left: 34px;
      }
      .put-text {
        float: right;
        margin-top: 40px;
        margin-right: 20px;
      }
    }
  }
}
</style>
